package capitulo10;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Random;

import javax.swing.JFrame;

import capitulo7.AnimatedSprite;

@SuppressWarnings("serial")
public class ThreatedLoop extends JFrame implements Runnable{
	int maxSpritesNumber = 5;
	AnimatedSprite[] sprites = new AnimatedSprite[maxSpritesNumber];
	
	//BufferedImage backBuffer;
	//Graphics2D g2d;
	
	Random rand = new Random();
	
	// The main Thread
	Thread thread;

	// count the number of rectangles drawn
	long count = 0, total = 0;

	public static void main(String[] args) {
		new ThreatedLoop();
	}

	public ThreatedLoop(){
		super("Simple Loop");
		setSize(500,400);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//backBuffer = new BufferedImage(500, 400, BufferedImage.TYPE_INT_RGB);
		//g2d = backBuffer.createGraphics();
		
		// Create new Sprites
		/*for (int i=0; i < maxSpritesNumber; i++){
			sprites[i] = new AnimatedSprite(this, _g2d);
			sprites[i].load("explosion.png", 6, 5, 128, 128);
			sprites[i].position = new Point(rand.nextInt(244)+128, rand.nextInt(144)+128);
			sprites[i].frameDelay = 5;
			sprites[i].totalFrames = 30;
		}*/
		
		
		thread = new Thread(this);
		thread.start();
	}
	
	@Override
	public void run() {
		long start = System.currentTimeMillis();
		
		// Acquire the current Thread
		Thread current = Thread.currentThread();
		
		// Here's the new game loop
		while(current==thread){
			try { Thread.sleep(0); }
			catch (InterruptedException e) { e.printStackTrace(); }
			
			// draw something
			repaint();
			
			// figure out how fast it's running
			if (start + 1000 < System.currentTimeMillis()){
				start = System.currentTimeMillis();
				total = count;
				count = 0;
			}
		}
	}
	
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		
		/*
		// Create a random rectangle
		int w = rand.nextInt(100);
		int h = rand.nextInt(100);
		int x = rand.nextInt(getSize().width - w);
		int y = rand.nextInt(getSize().height - h);
		Rectangle rect = new Rectangle(x,y,w,h);
		
		// Generate a random color
		int red = rand.nextInt(256);
		int green = rand.nextInt(256);
		int blue = rand.nextInt(256);
		g2d.setColor(new Color(red, green, blue));
		
		// draw the rectangle
		g2d.fill(rect); */
		
		sprites[0] = new AnimatedSprite(this, g2d);
		sprites[0].load("explosion.png", 6, 5, 128, 128);
		sprites[0].position = new Point(rand.nextInt(500-128), rand.nextInt(400-128));
		sprites[0].frameDelay = 5;
		sprites[0].totalFrames = 30;
		sprites[0].velocity = new Point((int)rand.nextInt(2)-1,(int)rand.nextInt(2)-1);
		sprites[0].rotationRate = 1.0;
		sprites[0].draw();

		// Add Another to the counter
		count++;
		
		g2d.setColor(Color.WHITE);
		g2d.fill(new Rectangle(0,360,500,400));
		g2d.setColor(Color.BLACK);
		g2d.drawString("Sprites per second: " + total, 10, 380);
	}
}
